package com.reader.cms.controller.sys;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.entity.sys.SysErrorLog;
import com.reader.cms.service.sys.ISysErrorLogService;
import com.reader.cms.controller.BaseController;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author jiahaikun
 * @date 2018-07-24
 */
@RestController
@RequestMapping("/api/syserrorlog")
public class SysErrorLogController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(SysErrorLogController.class);

    @Autowired
    public ISysErrorLogService iSysErrorLogService;


    /**
     * 分页查询数据
     * @param currentPage　当前页
     * @param size  每页几条
     * @param code　错误编码
     * @param message  错误信息
     * @param url   请求地址
     * @return
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage,
                                            @RequestParam(value = "size") int size,
                                            @RequestParam(value = "code", required = false) String code,
                                            @RequestParam(value = "message", required = false) String message,
                                            @RequestParam(value = "url", required = false) String url) {
        ResultMessageInfo resultMessage = null;
        Page<Map<String, Object>> list = iSysErrorLogService.selectListPage(currentPage, size, code, message, url);
        return ResultMessageInfo.ofSuccess("查询成功！",list);
    }

    /**
     * 添加新记录
     *
     * @param sysErrorLog 实体对象
     * @return
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody SysErrorLog sysErrorLog) {
        ValidatorUtil.validateEntity(sysErrorLog, AddGroup.class);//非空验证
        ResultMessageInfo resultMessage = null;
        boolean resultValue = iSysErrorLogService.insert(sysErrorLog);
        if (resultValue) {
            resultMessage = ResultMessageInfo.ofSuccess( "添加成功!", resultValue);
        } else {
            return ResultMessageInfo.ofMessage("添加失败!");
        }
        return resultMessage;
    }

    /**
     * 修改
     *
     * @param sysErrorLog 实体对象
     * @return
     */
    @PostMapping("update")
    public ResultMessageInfo update(@RequestBody SysErrorLog sysErrorLog) {
        ValidatorUtil.validateEntity(sysErrorLog, AddGroup.class);//非空验证
        ResultMessageInfo resultMessage = null;
        boolean resultValue = iSysErrorLogService.updateById(sysErrorLog);
        if (resultValue) {
            resultMessage = ResultMessageInfo.ofSuccess( "修改成功!", resultValue);
        } else {
            return ResultMessageInfo.ofMessage("删除失败!");
        }
        return resultMessage;
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return
     */
    @PostMapping("deleteIds")
    @Transactional
    @ApiOperation(value = "服务模块-推荐用户列表", notes = "保存用户选择的关注人信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "关注客户ID", required = true, dataType = "String", paramType = "path")})
    public ResultMessageInfo deleteBatchIds(@RequestBody String item) {
        ResultMessageInfo resultMessage = null;
        List<Long> ids = JSON.parseArray(JSON.parseObject(item).getString("ids"), Long.class);
        boolean resultValue = iSysErrorLogService.deleteBatchIds(ids);
        if (resultValue) {
            resultMessage = ResultMessageInfo.ofSuccess("删除成功!", resultValue);
        } else {
            return ResultMessageInfo.ofMessage("删除失败!");
        }
        return resultMessage;

    }
}